package com.example.linktop.newlistviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.linktop.newlistviewtest.model.Fruit;
import com.example.linktop.newlistviewtest.model.FruitAdapter;

import java.util.ArrayList;

public class FitstActivity extends AppCompatActivity {

    private ArrayList<Fruit> fruitList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fitst);
        fruitList = getFruitList();

        FruitAdapter adapter = new FruitAdapter(FitstActivity.this,R.layout.fruit_item_layout, fruitList);
        ListView listVeiw = (ListView)findViewById(R.id.fruit_listview);
        listVeiw.setAdapter(adapter);
        listVeiw.setOnItemClickListener(new fruitItemsOnclickListener());
    }


    private ArrayList<Fruit> getFruitList()  {

        if (fruitList == null) {
            ArrayList<Fruit> tmpList = new ArrayList<>();

            for (int i = 0; i < 30; i++ ) {

                tmpList.add(new Fruit("apple", R.mipmap.ic_launcher));

            }


            return tmpList;
        } else {

            return fruitList;
        }

    }

    private class fruitItemsOnclickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Fruit fru_tmp = fruitList.get(position);
            Toast.makeText(FitstActivity.this, "点击了 "+ position + " " +fru_tmp.getFruitName(), Toast.LENGTH_SHORT).show();

        }
    }


}
